//
//  DDHomeTableViewCell.swift
//  swift微博
//
//  Created by whitcast on 16/7/14.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import SnapKit

class DDHomeTableViewCell: UITableViewCell {
    //人物信息视图
    lazy var originalView: DDOriginalView = DDOriginalView()
    //评论\赞\转发视图
    lazy var toolbarView: DDToolBar = DDToolBar()
    //转发内容视图
    lazy var retweeted_statusView: DDStatusRetweetView = DDStatusRetweetView()
    //toolbarView的顶部约束
    var toolbarViewTop: Constraint?//要导入SnapKit,不然无限卡死xcode
    
    //ViewModel数据模型
    var statuesViewModel : DDStatuesViewModel?{
        didSet{
           //卸载约束
            toolbarViewTop?.uninstall()
            //赋值任务信息视图
            originalView.statuesViewModel = statuesViewModel
            //赋值给toolbar视图
            toolbarView.statuesViewModel = statuesViewModel
            //赋值给转发视图
            retweeted_statusView.statuesViewModel = statuesViewModel
            
            if statuesViewModel?.statuesModel?.retweeted_status != nil{
                //显示转发视图
                retweeted_statusView.hidden = false
                //更新约束
                toolbarView.snp_updateConstraints(closure: { (make) -> Void in
               self.toolbarViewTop = make.top.equalTo(retweeted_statusView.snp_bottom).constraint
                    
                })
            }else {
                //隐藏转发视图
                retweeted_statusView.hidden = true
                //更新约束
                toolbarView.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.toolbarViewTop = make.top.equalTo(originalView.snp_bottom).constraint
                    
                })
            }
        }
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
        
        backgroundColor = UIColor.whiteColor()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI和约束
    func setUpUI() {
        //间距
        let margin = 5
        //添加
        contentView.addSubview(originalView)
        
        contentView.addSubview(retweeted_statusView)
        
        contentView.addSubview(toolbarView)

        //约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top).offset(margin)
            make.leading.equalTo(contentView.snp_leading)
            make.trailing.equalTo(contentView.snp_trailing)
        }
        
        retweeted_statusView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom).offset(margin)
            make.leading.equalTo(originalView.snp_leading)
            make.trailing.equalTo(originalView.snp_trailing)
        }
        toolbarView.snp_makeConstraints { (make) -> Void in
           self.toolbarViewTop = make.top.equalTo(retweeted_statusView.snp_bottom).offset(margin).constraint
            make.leading.equalTo(originalView.snp_leading)
            make.trailing.equalTo(originalView.snp_trailing)
        }
        //使用contentView的约束,不能使用cell的,不然cell会重叠到一起
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(toolbarView.snp_bottom).offset(margin)
        }
    }
}
